package com.xjscrm.console.chat.core.impl.conversation;

import com.xjscrm.common.entity.WwCustomer;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.dto.ChatTagDTO;
import com.xjscrm.console.chat.frame.recv.RPullConversationInfoMsg;
import com.xjscrm.console.chat.frame.send.SPullConversationResultMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.wwCustomer.WwCustomerService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/17 09:42
 */
@Component
@Slf4j
public class ChatPullConversationInfoMsgHandler extends ChatBaseMsgHandler<RPullConversationInfoMsg> {

    @Autowired
    private WwCustomerService wwCustomerService;
    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Override
    public Class<RPullConversationInfoMsg> msgClass() {
        return RPullConversationInfoMsg.class;
    }

    @Override
    public boolean checkMsg(RPullConversationInfoMsg baseMsg) {
        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getTalkSessionId());
    }

    @Override
    public SResultMsg doHandle(RPullConversationInfoMsg content, ChannelHandlerContext ctx) {

        Long merId = content.getMerId();
        Long talkSessionId = content.getTalkSessionId();

        ChatTalkSession byId = chatTalkSessionService.getById(merId, talkSessionId);
        if (Objects.isNull(byId)) {
            throw new BusinessException("获取客户资料失败: 会话不存在");
        }
        WwCustomer byWxId = wwCustomerService.getByWxId(byId.getMerId(), byId.getDeviceUniqueId(), byId.getConversationId());
        if (Objects.isNull(byWxId)) {
            throw new BusinessException("获取客户资料失败: 客户不存在");
        }
        SPullConversationResultMsg sPullConversationResultMsg = new SPullConversationResultMsg();
        BeanUtils.copyProperties(byId, sPullConversationResultMsg);

        chatTalkSessionService.fillTag(byId, tag -> {
            if (Objects.equals(tag.getType(), 1)) {
                sPullConversationResultMsg.getCompanyTags().add(
                        new ChatTagDTO(tag.getId(), tag.getTagName())
                );
            } else if (Objects.equals(tag.getType(), 1)) {
                sPullConversationResultMsg.getSelfTags().add(
                        new ChatTagDTO(tag.getId(), tag.getTagName())
                );
            }
        }, tag1 -> {
            sPullConversationResultMsg.getSelfTags().add(
                    new ChatTagDTO(tag1.getId(), tag1.getName())
            );
        });
        return sPullConversationResultMsg;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_pull_conversation_info;
    }
}
